package com.ruoyi.project.question.ShowQues.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.question.compose.domain.QuestionnaireInfo;
import com.ruoyi.project.question.compose.domain.QuestionnaireInformantConn;
import com.ruoyi.project.question.compose.domain.QuestionnaireUserConn;
import com.ruoyi.project.question.compose.service.IQuestionnaireInfoService;
import com.ruoyi.project.question.compose.service.IQuestionnaireInformantConnService;
import com.ruoyi.project.question.compose.service.IQuestionnaireUserConnService;
import com.ruoyi.project.question.info.domain.QuestionInfo;
import com.ruoyi.project.question.info.service.IQuestionInfoService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: YancyZhang
 * @time: 2021/12/14 16:57
 */
@Controller
@RequestMapping("/question/showques")
public class ShowQuesController extends BaseController {
    private String prefix = "question/showques";

    @Autowired
    IQuestionnaireInfoService questionnaireInfoService;

    @Autowired
    IQuestionnaireUserConnService questionnaireUserConnService;

    @Autowired
    IQuestionnaireInformantConnService questionnaireInformantConnService;

    @Autowired
    IUserService userService;

    @Autowired
    IQuestionInfoService questionInfoService;

    @RequestMapping("")
    public String showques(){
        return prefix + "/index";
    }

    @RequestMapping("/single")
    public String single(@RequestParam("id") int questionnaireId,@RequestParam("id2") int informantId, Model model){
        //跳转详情页
        model.addAttribute("id",questionnaireId);
        model.addAttribute("id2",informantId);
        return prefix + "/SingleQuestionnaire";
    }

    @RequestMapping("/singleQuery")
    @ResponseBody
    public AjaxResult singleQuery(@RequestParam("id") int questionnaireId,@RequestParam("id2") int informantId){
        //id问卷id，查问卷里的信息，和被审查人id
        QuestionnaireInfo questionnaireInfo = questionnaireInfoService.selectQuestionnaireInfoById((long) questionnaireId);
        List<QuestionInfo> infoList = questionInfoService.selectQuestionInfoByQuestionnaireIDWithConnID((long) questionnaireId);
        User user = userService.selectUserById((long) informantId);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("questionnaireInfo",questionnaireInfo)
                .put("infoList", infoList)
                .put("user",user);
        return ajaxResult;
    }

    @RequestMapping("/allQuestionnaire")
    @ResponseBody
    public AjaxResult allList(@RequestParam("id") int id){
        //用户id查所有待填写问卷id
        System.out.println(id);
        List<QuestionnaireUserConn> questionnaireUserConnList = questionnaireUserConnService.selectUserInformantContact((long) id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("UserInformantList",questionnaireUserConnList);
        return ajaxResult;
    }


    @RequestMapping("/submit")
    @Transactional
    @ResponseBody
    public AjaxResult submitGrade(@RequestParam("informantId") Long informantId,
                                  @RequestParam("questionNaireId") Long questionnaireId,
                                  @RequestParam("grade") String grade){
        Long userId = ShiroUtils.getUserId();
        questionnaireInformantConnService.insertQuestionnaireInformantConn(new QuestionnaireInformantConn(questionnaireId,informantId,grade));
        questionnaireUserConnService.updateQuestionnaireUserConn(new QuestionnaireUserConn(userId,questionnaireId,informantId, 0L));
        return AjaxResult.success();
    }


}